package snsdbook.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import snsdbook.engine.PaginatedContentList;
import util.SQLUtil;
import util.Util;

/**
 * Servlet implementation class IndexServlet
 */
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  request.setAttribute("header_title", "WELKAM?");
	  
    // Get all ids of news
    String[] ids = SQLUtil.Query(
        "SELECT id_berita FROM (" +
        "SELECT id_berita, COUNT(id_likes) AS cnt FROM " +
        "(SELECT berita.id_berita, id_likes FROM berita, likes " +
        "WHERE berita.id_berita = likes.id_berita) AS A " +
        "GROUP BY id_berita ORDER BY cnt DESC LIMIT 0, 3) AS B " +
        "UNION " +
        "SELECT id_berita FROM (" +
        "SELECT id_berita, COUNT(id_komentar) AS cnt FROM " +
        "(SELECT berita.id_berita, id_komentar FROM berita, komentar " +
        "WHERE berita.id_berita = komentar.id_berita) AS C " +
        "GROUP BY id_berita ORDER BY cnt DESC LIMIT 0, 3) AS D");
    
    PaginatedContentList.Display(
        Util.toArrayInt(ids),
        /* show_searchbar =  */ false,
        /* show_comments = */ false,
        /* show_pagination = */ false,
        /* is_short = */ true,
        /* page = */ 0,
        request,
        response);
	}

}
